package com.client.service;

import com.client.Initialize;
import com.client.entity.B_client;
import com.client.utils.MessageUtils;
import com.client.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/*
 * 处理认证过程
 * HASH值暂时不管
 */
@Service
public class Certification {
    @Autowired
    KeyService keyService;
    @Autowired
    Sm9Service userService;
    @Autowired
    MigrationService migrationService;
    @Value("${ip}")
    private String ip;

    @Value("${udp.receive}")
    private Integer receive_port;

    /*
     * 发送认证请求--1
     * Request||IDB||time
     */
    public byte[] send1(String goal_id,UUID uid) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("type", "CERTIFICATION_ONE");
        map.put("id", keyService.getId());
        map.put("time", MessageUtils.getNowTime());
        map.put("port", receive_port);
        map.put("uid", uid);
        map.put("uuid", MessageUtils.getuuid());
        map.put("hash", MessageUtils.HashOfCertification(map));
        return Utils.getByteFromObjectMap(map);
    }

    /*
     * 发送认证请求--2
     *E(Reply||IDA||N1||time||HASH,IDB)
     */
    public byte[] send2(String goal_id, int my_n, B_client client) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("type", "CERTIFICATION_TWO");
        map.put("id", keyService.getId());
        map.put("uid", client.getUid());
        map.put("uuid", MessageUtils.getuuid());
        map.put("n", my_n);   //随机数
        map.put("time", MessageUtils.getNowTime());
        map.put("hash", MessageUtils.HashOfCertification(map));
        byte[] bytes = Utils.getByteFromObjectMap(map);
        if (!Initialize.HPS)
            return userService.Encrypt(goal_id, bytes);
        else {
            migrationService.Encrypt(goal_id,client.getUid(), bytes);
        }
        return null;
    }

    /*
     * 发送认证请求--3
     *E(f(N1)||N2||time||HASH, IDA)
     */
    public byte[] send3(String goal_id, int n, int my_n, UUID uid) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("type", "CERTIFICATION_THREE");
        map.put("id", keyService.getId());
        map.put("uid", uid);
        map.put("uuid", MessageUtils.getuuid());
        map.put("f_n", n % 25);   //双方事先约定的变换
        map.put("n", my_n);   //随机数
        map.put("time", MessageUtils.getNowTime());
        map.put("hash", MessageUtils.HashOfCertification(map));
        byte[] bytes = Utils.getByteFromObjectMap(map);

        if (!Initialize.HPS)
            return userService.Encrypt(goal_id, bytes);
        else {
            migrationService.Encrypt(goal_id,uid, bytes);
        }
        return null;
    }

    /*
     * 发送认证请求--4
     *E(confirm||f(N2)||time||HASH, IDB)
     */
    public byte[] send4(String goal_id, int n,UUID uid) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("type", "CERTIFICATION_FOUR");
        map.put("id", keyService.getId());
        map.put("f_n", n % 25);   //双方事先约定的变换
        map.put("time", MessageUtils.getNowTime());
        map.put("uid", uid);
        map.put("uuid", MessageUtils.getuuid());
        map.put("hash", MessageUtils.HashOfCertification(map));
        byte[] bytes = Utils.getByteFromObjectMap(map);

        if (!Initialize.HPS)
            return userService.Encrypt(goal_id, bytes);
        else {
            migrationService.Encrypt(goal_id,uid, bytes);
        }
        return null;
    }

    /*
     * 发送认证请求--5
     *E(confirm||m ||time|| HASH, IDA)
     */
    public byte[] send5(String goal_id,UUID uid) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("type", "CERTIFICATION_FIVE");
        map.put("id", keyService.getId());
        map.put("uuid", MessageUtils.getuuid());
        map.put("uid", uid);
        map.put("time", MessageUtils.getNowTime());
        map.put("hash", MessageUtils.HashOfCertification(map));

        byte[] bytes = Utils.getByteFromObjectMap(map);
        if (!Initialize.HPS)
            return userService.Encrypt(goal_id, bytes);
        else {
            migrationService.Encrypt(goal_id,uid, bytes);
        }
        return null;
    }


}
